<?php

namespace app\api\logic;

use app\common\model\HistoryQuestionDate;

class HistoryQuestion
{
    /**
     * 查询列表
     * @param $param
     * @return array
     */
    public function getProfession($param): array
    {
        // 提取参数
        $course_id = $param['course_id'] ?? 0;
        $profession_id = $param['professions_id'];

        // 查询条件
        $where = [['a.status', '=', 1]];
        if ($course_id) {
            $where[] = ['a.id', '=', $course_id];

            // 输出字段
            $field = 'a.id as course_id,a.code,a.name';

            // 查询数据
            $list = app(\app\common\model\Course::class)
                ->alias('a')
                ->field($field)
                ->where($where)
                ->select();
            $list = $list ? $list->toArray() : [];
        } else {
            if ($profession_id) {
                $where[] = ['a.profession_id', '=', $profession_id];

                // 输出字段
                $field = 'a.course_id,a.code,a.name';

                // 查询数据
                $list = app(\app\common\model\ProfessionCourse::class)
                    ->alias('a')
                    ->field($field)
                    ->where($where)
                    ->select();
                $list = $list ? $list->toArray() : [];
            }
        }

        // 提取关联考试分组信息
        $courseIds = array_column($list, 'course_id');
        if ($courseIds) {
            $history = new HistoryQuestionDate();
            $historyDate = $history->getListByCourseIds($courseIds);
            $historyDateCate = array_cate($historyDate, 'course_id');
        }

        // 组装数据
        foreach ($list as $key => $val) {
            $list[$key]['exams_real_paper'] = $historyDateCate[$val['course_id']] ?? [];
            $list[$key]['isOpen'] = 0;
        }

        // 返回
        return $list;
    }

}